/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.os.dao.ephimov.memorydao;

import edu.os.dao.ephimov.interfaces.ApplicationDAO;
import edu.os.dao.ephimov.transferobjects.Application;
import edu.os.dao.ephimov.memorydao.entities_database.APPLICATION;


import edu.os.dao.ephimov.Mylogger;

import java.util.*;
/**
 *
 * @author shalash
 */
public class MemoryApplication implements ApplicationDAO  {
    
    private List<APPLICATION> lApplication;
    private int               countID;
    
    MemoryApplication() {
        lApplication = new ArrayList<APPLICATION>();
        countID = -1;
    }
    
    @Override
    public int addApplication(Application app) {
        countID++;
        APPLICATION app_ent = new APPLICATION();
        
        app_ent.ID_APPLICATION          = countID;
        app_ent.NAME                    = app.getName();
        app_ent.DESCRIPTION             = app.getDiscription();
        app_ent.LANGUAGE                = app.getLanguage();
        app_ent.OPERATION_SYSTEMS       = app.getOperationSystems();
        app_ent.PUBLICATION_DATE        = app.getPublicationDate();
        app_ent.VERSION_MODIFICATION    = app.getVersionModification();
        app_ent.DIRECTIONS_FOR_DELIVERY = app.getDirectionsForDelivery();
        app_ent.PRICE                   = app.getPrice();
        app_ent.ID_USER                 = app.getIdUser();
        app_ent.STATUS_APPLICATION      = app.getStatusApplication();
        //app_ent.APPLICATION_FILE        = new char[100]; //Сделать приём файла
        
        lApplication.add(app_ent);
        
        return countID;
    }
    
    @Override
    public Application findApplication(Application App) { 
       
        Iterator<APPLICATION> itr = lApplication.iterator();
        APPLICATION app;
        while (itr.hasNext()) {
            app = itr.next();
            if ((app.ID_APPLICATION == App.getIdApplication()) || (app.NAME.equals(App.getName()))) {
               Application  result = new Application(); 
               result.setIdApplication(app.ID_APPLICATION);
               result.setName(app.NAME);
               result.setDiscription(app.DESCRIPTION);
               result.setDirectionsForDelivery(app.DIRECTIONS_FOR_DELIVERY);
               result.setIdUser(app.ID_USER);
               result.setLanguage(app.LANGUAGE);
               result.setOperationSystems(app.OPERATION_SYSTEMS);
               result.setPrice(app.PRICE);
               result.setPublicationDate(app.PUBLICATION_DATE);
               result.setStatusApplication(app.STATUS_APPLICATION);
               result.setVersionModification(app.VERSION_MODIFICATION);
               return result;
            }                
        }
        return null;
    }

    @Override
    public List<Application> getAllApplications() {
        Iterator<APPLICATION> itr = lApplication.iterator();
        List<Application> result = new ArrayList<Application>();
        Application toApplication;                
        
        while (itr.hasNext()) {
            APPLICATION app = itr.next();
            toApplication = new Application();
            toApplication.setIdApplication(app.ID_APPLICATION);
            toApplication.setName(app.NAME);
            toApplication.setDiscription(app.DESCRIPTION);
            toApplication.setDirectionsForDelivery(app.DIRECTIONS_FOR_DELIVERY);
            toApplication.setIdUser(app.ID_USER);
            toApplication.setLanguage(app.LANGUAGE);
            toApplication.setOperationSystems(app.OPERATION_SYSTEMS);
            toApplication.setPrice(app.PRICE);
            toApplication.setPublicationDate(app.PUBLICATION_DATE);
            toApplication.setStatusApplication(app.STATUS_APPLICATION);
            toApplication.setVersionModification(app.VERSION_MODIFICATION);  
            
            result.add(toApplication);
        }
        
        
        return result;
    }
        
    
    @Override
    public int deleteApplication(int IdApplication) {
        Iterator<APPLICATION> itr = lApplication.iterator();
        while (itr.hasNext()) {
            if (itr.next().ID_APPLICATION == IdApplication) {
                itr.remove();
                return 0;
            }
        }
        return -1;
    }
    
    @Override
    public int updateApplicatoin(Application App) {
        
        APPLICATION app_ent = new APPLICATION();
        
        Iterator<APPLICATION> itr = lApplication.iterator();
        APPLICATION app;
        while (itr.hasNext()) {
            app = itr.next();
            new Mylogger().writeLog(App.getClass().getName() + " -- If");
            if ((app.ID_APPLICATION == App.getIdApplication()) || (app.NAME.equals(App.getName()))) {      
                app_ent.ID_APPLICATION          = App.getIdApplication();
                app_ent.NAME                    = App.getName();
                app_ent.DESCRIPTION             = App.getDiscription();
                app_ent.LANGUAGE                = App.getLanguage();
                app_ent.OPERATION_SYSTEMS       = App.getOperationSystems();
                app_ent.PUBLICATION_DATE        = App.getPublicationDate();
                app_ent.VERSION_MODIFICATION    = App.getVersionModification();
                app_ent.DIRECTIONS_FOR_DELIVERY = App.getDirectionsForDelivery();
                app_ent.PRICE                   = App.getPrice();
                app_ent.ID_USER                 = App.getIdUser();
                app_ent.STATUS_APPLICATION      = App.getStatusApplication();
                //app_ent.APPLICATION_FILE        = new char[100]; //Сделать приём файла
        
                new Mylogger().writeLog(App.getClass().getName() + " -- " + "if execute");
                
                itr.remove();
                lApplication.add(app_ent);
                
                
                
            }
            return 0;
        }
        
        return -1;
        
    }
}
